package com.easyapi.service.execute.impl;


import com.alibaba.druid.pool.DruidDataSource;
import com.easyapi.common.Constants;
import com.easyapi.common.bean.ApiCenterDatasource;
import com.easyapi.common.bean.ApiCenterInterfaceInfo;
import com.easyapi.common.emun.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.common.utils.BaseResponse;
import com.easyapi.common.utils.ResultUtils;
import com.easyapi.config.PoolConfig;
import com.easyapi.mapper.ApiCenterDatasourceMapper;
import com.easyapi.mapper.ApiCenterInterfaceInfoMapper;
import com.easyapi.utils.CaffeineCacheUtil;
import com.easyapi.utils.RedisCache;
import org.apache.commons.collections4.MapUtils;
import org.springframework.boot.configurationprocessor.json.JSONArray;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Future;

/**
 * @author javadadi
 * 自定义sql 执行类
 */
//@Service
public class ConfigSqlApiExecuteServiceImpl {

    @Resource
    private RedisCache redisCache;

    @Resource
    private ApiCenterInterfaceInfoMapper apiMapper;
    @Resource
    private ApiCenterDatasourceMapper datasourceMapper;

    /**
     * 自定义sql
     *
     * @param path
     * @param paramMap
     * @return
     */
    public BaseResponse<Object> execute(String path, Map<String, String> paramMap) {
        ApiCenterInterfaceInfo interfaceInfo = getInterfaceInfo(path);
        if (Objects.isNull(interfaceInfo)){
            throw new BizException(StatusCode.NO_SUCH_API);
        }
        Long datasourceId = interfaceInfo.getDatasourceId();
        ApiCenterDatasource datasource = datasourceMapper.selectById(datasourceId);
        if (Objects.isNull(datasource)){
            throw new BizException(StatusCode.NO_SUCH_DATASOURCE);
        }
        String dataName = datasource.getDataName();
        DruidDataSource druidDataSource = (DruidDataSource) CaffeineCacheUtil.get(dataName + "_" + datasource.getId());
        if (Objects.isNull(druidDataSource)) {
            return ResultUtils.success(null);
        }
        //线程池提交任务
       Future<JSONArray> future =  PoolConfig.RUN_SYNC_JOB_POOL.submit(()-> new JSONArray());
        return null;
    }

    @SuppressWarnings("all")
    private ApiCenterInterfaceInfo getInterfaceInfo(String path) {
        Map<String, ApiCenterInterfaceInfo> apiMap = CaffeineCacheUtil.get(Constants.INTERFACE_PATH_MAP, Map.class);
        if (MapUtils.isNotEmpty(apiMap)) {
            if (apiMap.containsKey(path)) {
                return apiMap.get(path);
            }
        } else if (redisCache.hasKey(Constants.INTERFACE_PATH_MAP)) {
            Map<String, ApiCenterInterfaceInfo> pathMap = (Map<String, ApiCenterInterfaceInfo>) redisCache.get(Constants.INTERFACE_PATH_MAP);
            if (path.contains(path)) {
                return pathMap.get(path);
            }
        }
        ApiCenterInterfaceInfo interfaceInfo = apiMapper.getByApiPath(path);
        if (Objects.isNull(interfaceInfo)) {
            throw new BizException(StatusCode.NO_SUCH_API);
        }
        return interfaceInfo;
    }
}
